//
//  ViewController.m
//  TestWine
//
//  Created by 吴志勇 on 2020/2/26.
//  Copyright © 2020 wzy. All rights reserved.
//

#import "ViewController.h"
#import "WineTableViewCell.h"
#import "Wine.h"
//遵循代理
/**
@interface ViewController()<WineTableViewCellDelegate>

@end
 */

@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *totalPricelLabel;

/** 酒模型 */
@property (nonatomic, strong) NSArray *wineArray;
@property (weak, nonatomic) IBOutlet UIButton *buyButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;


/** 优化加一个数组保存买过的酒 */
@property (nonatomic, strong) NSMutableArray *shoppingCar;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(plusClick:) name:@"plusClickNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(minusClick:) name:@"minusClickNotification" object:nil];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    
    //KVO移除
//    for (Wine *wine in _wineArray) {
//        [wine removeObserver:self forKeyPath:@"count"];
//    }
}

#pragma mark----监听通知
- (void)plusClick:(NSNotification *)note{
    Wine *wine = note.object;
    self.totalPricelLabel.text =[NSString stringWithFormat:@"%d",self.totalPricelLabel.text.intValue + wine.money.intValue];
    self.buyButton.enabled = YES;
    
    if (![self.shoppingCar containsObject:wine]) {
        [self.shoppingCar addObject:wine];
    }
    

}

- (void)minusClick:(NSNotification *)note{
     Wine *wine = note.object;
    self.totalPricelLabel.text =[NSString stringWithFormat:@"%d",self.totalPricelLabel.text.intValue - wine.money.intValue];
    if (self.totalPricelLabel.text.intValue == 0) {
        self.buyButton.enabled = NO;
    }
    
    if (wine.count == 0) {
        [self.shoppingCar removeObject:wine];
    }
}


#pragma mark -lazyMethod
- (NSMutableArray *)shoppingCar{
    if (!_shoppingCar) {
        _shoppingCar = [NSMutableArray array];
    }
    return _shoppingCar;
}

- (NSArray *)wineArray{

    if (!_wineArray) {
        NSArray  *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"wine.plist" ofType:nil]];
        NSMutableArray *temp = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            [temp addObject:[Wine wineWithDict:dict]];
        }
        _wineArray = temp;
        
        
        
        //KVO,使用KVO默认会生成一个子类
        /*
        *for (Wine *wine in _wineArray) {
             NSLog(@"%@",[wine valueForKeyPath:@"isa"]); //w未使用KVO - Wine，isa 指向真实类型
            [wine addObserver:self forKeyPath:@"count" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
             NSLog(@"%@",[wine valueForKeyPath:@"isa"]);//使用KVO后 - NSKVONotifying_Wine，私有属性不可访问
        
        }
        */
    }
    
    
    return _wineArray;
}

 //KVO
/**

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    int new = [change[NSKeyValueChangeNewKey] intValue];
    int old = [change[NSKeyValueChangeOldKey] intValue];
    if (new > old) {
        //加号
    }else{
        //减号
    }
}
 */

#pragma mark---UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.wineArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"wineCell";
    WineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    //把模型传过去
    cell.wine = self.wineArray[indexPath.row];
//    cell.delegate = self;
    return cell;
}

#pragma mark --- 按钮事件
- (IBAction)clear {
    self.totalPricelLabel.text = @"0";
    for (Wine *wine in self.wineArray) {
        wine.count = 0;
    }
    self.buyButton.enabled = NO;
    [self.tableView reloadData];
    
    //用KVO的时候count发生变化，总价会出错。需要把总价置0；
//    self.totalPricelLabel.text = @"0"
    
    
    //优化
    
//    for (Wine *wine in self.shoppingCar) {
//        wine.count = 0;
//    }
//    [self.tableView reloadData];
//    self.totalPricelLabel.text = @"0";
//    [self.shoppingCar removeAllObjects];
//     self.buyButton.enabled = NO;
}


- (IBAction)pay {
    int totalPrice = 0;
    for (Wine *wine in self.shoppingCar) {
        if (wine.count != 0) {
            NSLog(@"购买了[%@][%@]瓶，花费了:¥%ld",wine.name,[NSString stringWithFormat:@"%ld",wine.count],wine.money.integerValue * wine.count);
            totalPrice +=wine.money.integerValue * wine.count;
        }
    }
    NSLog(@"总花费:¥%d",totalPrice);
   
}

#pragma mark---- WineTableViewCellDelegate
/**
- (void)WineTableViewCellDidMinusButton:(WineTableViewCell *)cell{
}

- (void)WineTableViewCellDidPlusButton:(WineTableViewCell *)cell{
}
 */


@end
